<?php

/**
 * Model Country
 *
 * @author nguyenmanhluu
 * @since Jul 29, 2013 - 12:23:30 PM
 *
 * The followings are the available columns in table '{{country}}':
 * @property integer $id
 * @property string $name
 * @property string $iso_code_2
 * @property string $iso_code_3
 * @property string $address_format
 * @property integer $postcode_required
 * @property integer $status
 */
class Country extends CActiveRecord
{

    const STATUS_PUBLIC   = 1;
    const STATUS_DRAF     = 0;
    const DEFAULT_COUNTRY = 'SGP';

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Country the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{country}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, iso_code_2, iso_code_3, address_format, postcode_required', 'required'),
            array('postcode_required, status', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 128),
            array('iso_code_2', 'length', 'max' => 2),
            array('iso_code_3', 'length', 'max' => 3),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, iso_code_2, iso_code_3, address_format, postcode_required, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id'                => 'ID',
            'name'              => Yii::t('main', 'Name'),
            'iso_code_2'        => Yii::t('main', 'Iso Code 2'),
            'iso_code_3'        => Yii::t('main', 'Iso Code 3'),
            'address_format'    => Yii::t('main', 'Address Format'),
            'postcode_required' => Yii::t('main', 'Postcode Required'),
            'status'            => Yii::t('main', 'Status'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('iso_code_2', $this->iso_code_2, true);
        $criteria->compare('iso_code_3', $this->iso_code_3, true);
        $criteria->compare('address_format', $this->address_format, true);
        $criteria->compare('postcode_required', $this->postcode_required);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * @uses at rules() in Customer.php
     * @return mixed list of iso_code_3 of countries
     */
    public static function get_arr_country()
    {
        $criteria         = new CDbCriteria();
        $criteria->select = array('status', 'iso_code_3', 'id');

        $criteria->condition = 'status=:t';
        $criteria->params    = array(':t' => self::STATUS_PUBLIC);

        $contries = self::model()->findAll($criteria);

        $ret = array();

        if (!$contries)
            return $ret;

        foreach ($contries as $item)
        {
            $ret[] = $item->iso_code_3;
        }

        return $ret;
    }

    /**
     * 
     * @return mixed get list of country with iso_code_3
     */
    public static function getAllCountry()
    {
        $criteria         = new CDbCriteria();
        $criteria->select = array('status', 'iso_code_3', 'id', 'name');

        $criteria->condition = 'status=:t';
        $criteria->params    = array(':t' => self::STATUS_PUBLIC);

        $contries = self::model()->findAll($criteria);

        $ret = array();

        if (!$contries)
            return $ret;

        foreach ($contries as $item)
        {
            $ret[$item->iso_code_3] = $item->name;
        }
        return $ret;
    }

    public static function getCountrytByCode($iso_code_3)
    {
        $cri            = new CDbCriteria();
        $cri->condition = 'iso_code_3 = :code';
        $cri->params    = array(
            ':code' => $iso_code_3,
        );

        $country = Country::model()->find($cri);

        if ($country != null)
        {
            return $country;
        }
        else
        {
            return null;
        }
    }

}

/** 
 * End file Country
 */
